package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ImportErpMapping;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.importerpmapping.ImportErpMappingAddReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.importerpmapping.ImportErpMappingEditReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.importerpmapping.ImportErpMappingListReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.importerpmapping.ImportErpMappingListResVo;
import com.zmj.sy.mom.srv.aps.mapper.ImportErpMappingMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@RequiredArgsConstructor
@Service
public class ImportErpMappingService {

    private final ImportErpMappingMapper importErpMappingMapper;


    public BasePageResVo<ImportErpMappingListResVo> list(ImportErpMappingListReqVo reqVo) {
        LambdaQueryWrapper<ImportErpMapping> wrapper = Wrappers.<ImportErpMapping>lambdaQuery();
        wrapper.like(StringUtils.hasText(reqVo.getErpOpCode()), ImportErpMapping::getErpOpCode, reqVo.getErpOpCode());
        wrapper.like(StringUtils.hasText(reqVo.getErpOp()), ImportErpMapping::getErpOp, reqVo.getErpOp());
        wrapper.like(StringUtils.hasText(reqVo.getMesWorkPhase()), ImportErpMapping::getMesWorkPhase, reqVo.getMesWorkPhase());
        wrapper.like(StringUtils.hasText(reqVo.getMesWorkCenter()), ImportErpMapping::getMesWorkCenter, reqVo.getMesWorkCenter());
        wrapper.like(StringUtils.hasText(reqVo.getErpPhaseName()), ImportErpMapping::getErpPhaseName, reqVo.getErpPhaseName());
        wrapper.like(StringUtils.hasText(reqVo.getApsPhaseName()), ImportErpMapping::getApsPhaseName, reqVo.getApsPhaseName());
        wrapper.orderByDesc(ImportErpMapping::getId);
        Page<ImportErpMapping> erpMesOpMappingPage = importErpMappingMapper.selectPage(reqVo.toPage(), wrapper);

        return BasePageResVo.of(erpMesOpMappingPage, ImportErpMappingListResVo.class);
    }

    @Transactional
    public void add(ImportErpMappingAddReqVo reqVo) {
        ImportErpMapping importErpMapping = MkBeanUtils.copyProperties(reqVo, ImportErpMapping.class);
        importErpMappingMapper.insert(importErpMapping);
    }

    @Transactional
    public void edit(ImportErpMappingEditReqVo reqVo) {
        ImportErpMapping importErpMapping = MkBeanUtils.copyProperties(reqVo, ImportErpMapping.class);
        importErpMappingMapper.updateById(importErpMapping);
    }

    @Transactional
    public void delete(BaseIdReqVo reqVo) {
        importErpMappingMapper.deleteById(reqVo.getId());
    }
}
